/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka.admin;

import apteka.CategoryManager;
import apteka.ProductManager;
import apteka.tables.Product;
import java.io.File;
import java.util.Calendar;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Marek
 */

@Controller
@RequestMapping("/admin_panel/product.htm")
public class AdminProductController {

    @Autowired
        CategoryManager cat_man;
    @Autowired
        ProductManager prod_man;
    @Autowired
        AdminProductValidator productValidator;


    @RequestMapping(method = RequestMethod.GET)
    public String showPage(Map model) {
        model.put("categoryList", cat_man.getCategories());
        Product product = new Product();
        model.put("photo_path",prod_man.getPhotoUploadPath());
        model.put("product",product);
        model.put("page", "product.jsp");
        return "admin_panel/main";
    }

    @RequestMapping(params="op=edit",method = RequestMethod.GET)
    public String showPage(@RequestParam int id, Map model) {
        model.put("edit",true);
        model.put("photo_path",prod_man.getPhotoUploadPath());
        model.put("categoryList", cat_man.getCategories());
        model.put("product",prod_man.getProduct(id));
        model.put("page", "product.jsp");
        return "admin_panel/main";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(Product product,BindingResult result,Map model) {
        
        productValidator.validate(product, result);
        if (!result.hasErrors()) {
            String filename = product.getFileData().getOriginalFilename();
            if (product.getIdproduct()!=-1) {
                if (product.getPicturelink().compareTo(product.getFileData().getOriginalFilename())==1) {
                    File file = new File(prod_man.getPhotoUploadPath()+product.getPicturelink());
                    if (!file.isDirectory()) file.delete();
                }
            }
            File file = new File(prod_man.getPhotoUploadPath()+filename);
            try {
                file.createNewFile();
                product.getFileData().transferTo(file);
                ImageResizer resizer = new ImageResizer();
                resizer.resizeFile(file, 250);
                product.setPicturelink(filename);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            if (product.getIdproduct()==-1) {
                Calendar calen = Calendar.getInstance();
                product.setAddDate(calen.getTime());
                prod_man.addProduct(product);
            } else {
                prod_man.editProduct(product);
            }

            return "redirect:products.htm";
        }
        model.put("categoryList", cat_man.getCategories());
        model.put("page", "product.jsp");
        return "admin_panel/main";
    }
    
}
